package kr.hkit.PowerJava.chapter16;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.BoxLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Traffic extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JButton btnNewButton;
	boolean flag = false;
    private int light_number = 0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Traffic frame = new Traffic();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Traffic() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		btnNewButton = new JButton("traffic light turn on");
		btnNewButton.addActionListener(this);
		contentPane.add(btnNewButton, BorderLayout.SOUTH);
	}
protected void paintComponent(Graphics g){
	super.paintComponents(g);
	g.setColor(Color.BLACK);
	g.drawOval(100, 100, 100, 100);
	g.drawOval(100, 200, 100, 100);
	g.drawOval(100, 300, 100, 100);
	if (light_number == 0){
		g.setColor(Color.RED);
		g.fillOval(100, 100, 100, 100);
	}else if (light_number == 1){
		g.setColor(Color.GREEN);
		g.drawOval(100, 200, 100, 100);
	}else{
		g.setColor(Color.YELLOW);
		g.drawOval(100, 300, 100, 100);
	}
	
}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnNewButton) {
			actionPerformedBtnNewButton(e);
		}
	}
	protected void actionPerformedBtnNewButton(ActionEvent e) {
	if (++light_number >= 3){
		light_number = 0;
		repaint();
	}
	}
}
